package com.spring.mvc.config.vo;

/**
 * 请求响应结果
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2017-08-18 10:18
 **/
public class ResultVo<T> {

    /**
     * 请求成功
     */
    private static final Integer REQUEST_SUCCESS = 200;
    /**
     * 请求成功-获取响应数据为空
     */
    private static final Integer REQUEST_SUCCESS_RESPONSE_NO_DATA = 201;
    /**
     * 请求失败-获取响应数据为空
     */
    private static final Integer REQUEST_SUCCESS_RESPONSE_NO_DATA_A = 202;
    /**
     * 请求失败
     */
    private static final Integer REQUEST_PARAM_FAILURE = 400;
    /**
     * 请求为授权
     */
    private static final Integer REQUEST_UNAUTHORIZED = 401;
    /**
     * 请求错误
     */
    private static final Integer REQUEST_SERVER_ERROR = 500;

    private Integer code;
    private String msg;
    private T data;

    /**
     * 请求成功无数据返回
     *
     * @return ResponseResult
     */
    public static ResultVo success() {
        ResultVo result = new ResultVo();
        result.setMsg("请求成功!");
        result.setCode(REQUEST_SUCCESS);
        return result;
    }

    /**
     * 请求成功有数据返回
     *
     * @param data 返回数据
     * @return ResponseResult
     */
    public static ResultVo success(Object data) {
        ResultVo result = new ResultVo();
        result.setData(data);
        result.setCode(REQUEST_SUCCESS);
        result.setMsg("请求成功!");
        return result;
    }

    /**
     * 请求成功-获取响应数据为空
     *
     * @return ResponseResult
     */
    public static ResultVo successResultIsNull() {
        ResultVo result = new ResultVo();
        result.setCode(REQUEST_SUCCESS_RESPONSE_NO_DATA);
        result.setMsg("请求成功,查询数据为空!");
        return result;
    }

    /**
     * 请求参数错误
     *
     * @return ResponseResult
     */
    public static ResultVo paramError() {
        ResultVo result = new ResultVo();
        result.setCode(REQUEST_PARAM_FAILURE);
        result.setMsg("请求参数异常!");
        return result;
    }

    /**
     * 请求服务器错误
     *
     * @return ResponseResult
     */
    public static ResultVo serverError() {
        ResultVo result = new ResultVo();
        result.setCode(REQUEST_SERVER_ERROR);
        result.setMsg("服务器繁忙,请稍后再试.");
        return result;
    }

    /**
     * 请求未授权
     *
     * @return InvokeResult
     */
    public static ResultVo unauthorizedError() {
        ResultVo result = new ResultVo();
        result.setCode(REQUEST_UNAUTHORIZED);
        result.setMsg("无访问权限或token失效!");
        return result;
    }

    /**
     * 自定义返回
     *
     * @param code 响应码
     * @param msg  响应提示信息
     * @param data 返回数据
     * @return InvokeResult
     */
    public static ResultVo customResult(Integer code, String msg, Object data) {
        ResultVo result = new ResultVo();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 业务错误
     *
     * @param code 响应码
     * @param msg  响应提示信息
     * @return InvokeResult
     */
    public static ResultVo businessError(Integer code, String msg) {
        ResultVo result = new ResultVo();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
